﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar;
using System.Windows.Forms;
using BLL;
using Entities;
using WindowsApp.QLLopHoc;
using WindowsApp.QLMonHoc;
using WindowsApp.QLGiaoVien;

namespace WindowsApp.QLKeHoachGiangDay
{
    public partial class frmAddChiTietKHGD : DevComponents.DotNetBar.Office2007Form
    {
        public frmAddChiTietKHGD()
        {
            InitializeComponent();
        }
         private ChiTietKeHoachGiangDayManager _manager;
        private ChiTietKeHoachGiangDay _ChiTietKHGD;
        private bool _isUpdate;
        public string _maQL;
          frmAddQuanLyKHGD cha;

        public frmAddChiTietKHGD(ChiTietKeHoachGiangDayManager _man,frmAddQuanLyKHGD p ,ChiTietKeHoachGiangDay _chitiet = null) : this()
        {
            _manager = _man;
            _ChiTietKHGD = _chitiet;
            cha=p;
            _isUpdate = (_chitiet != null);
        
        }
        private void frmAddChiTietKHGD_Load(object sender, EventArgs e)
        {
            LoadCBB();
            if (_isUpdate)
            {
                txtMaKH.Text = _ChiTietKHGD.MaKH;
                cbbMonHoc.SelectedValue = _ChiTietKHGD.MaMH;
                cbbLopHoc.SelectedValue = _ChiTietKHGD.MaLop;
                cbbGiaoVien.SelectedValue = _ChiTietKHGD.MaGV;
                if (_ChiTietKHGD.GhepLop == "")
                    cbbGhepLop.SelectedIndex = -1;
                else
                     cbbGhepLop.SelectedValue = _ChiTietKHGD.GhepLop;
                mtbSoTietLTDaDay.Text = _ChiTietKHGD.SoTietLTDaDay.ToString();
                mtbSoTietLTConLai.Text = _ChiTietKHGD.SoTietLTConLai.ToString();
                mtbSoTietTHDaDay.Text = _ChiTietKHGD.SoTietTHDaDay.ToString();
                mtbSoTietTHConLai.Text = _ChiTietKHGD.SoTietTHConLai.ToString();
                mtbSoTuanConLai.Text = _ChiTietKHGD.SoTuanConLai.ToString();
                
            }
            txtMaKH.ReadOnly = _isUpdate;
        }
        public void LoadCbbGhepLop()
        {
            List<LopHoc> ds = LopHoc.Objects.GetAll().ToList();
            cbbGhepLop.DataSource = ds;
            cbbGhepLop.DisplayMember = "MaLop";
            cbbGhepLop.ValueMember = "MaLop";
            cbbGhepLop.SelectedIndex = -1;
        }
      
        public void LoadCbbMonHoc()
        {
            List<MonHoc> ds = MonHoc.Objects.GetAll().ToList();
            cbbMonHoc.DataSource = ds;
            cbbMonHoc.DisplayMember = "TenMH";
            cbbMonHoc.ValueMember = "MaMH";
        }
        public void LoadCbbLopHoc()
        {
            List<LopHoc> ds = LopHoc.Objects.GetAll().ToList();
            cbbLopHoc.DataSource = ds;
            cbbLopHoc.DisplayMember = "MaLop";
            cbbLopHoc.ValueMember = "MaLop";


        }
        public void LoadCbbGiaoVien()
        {
            List<GiaoVien> ds = GiaoVien.Objects.GetAll().ToList();
            cbbGiaoVien.DataSource = ds;
            cbbGiaoVien.DisplayMember = "HoTen";
            cbbGiaoVien.ValueMember = "MaGV";

        }
        public void LoadCBB()
        {
            LoadCbbMonHoc();            
            LoadCbbLopHoc();           
            LoadCbbGhepLop();
            LoadCbbGiaoVien();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_isUpdate) _ChiTietKHGD = new ChiTietKeHoachGiangDay();

            _ChiTietKHGD.MaKH = txtMaKH.Text;
            _ChiTietKHGD.MaLop = cbbLopHoc.SelectedValue.ToString();
           if (cbbGhepLop.SelectedIndex!=-1)
                _ChiTietKHGD.GhepLop = cbbGhepLop.SelectedValue.ToString();
           else _ChiTietKHGD.GhepLop = "";
            _ChiTietKHGD.MaGV = cbbGiaoVien.SelectedValue.ToString();
            _ChiTietKHGD.MaMH = cbbMonHoc.SelectedValue.ToString();

            _ChiTietKHGD.SoTietLTDaDay =Convert.ToInt32(mtbSoTietLTDaDay.Text);
            _ChiTietKHGD.SoTietLTConLai = Convert.ToInt32(mtbSoTietLTConLai.Text);
            _ChiTietKHGD.SoTietTHDaDay = Convert.ToInt32(mtbSoTietTHDaDay.Text);
            _ChiTietKHGD.SoTietTHConLai = Convert.ToInt32(mtbSoTietTHConLai.Text);
            _ChiTietKHGD.SoTuanConLai = Convert.ToInt32(mtbSoTuanConLai.Text);
            _ChiTietKHGD.MaQL = _maQL;

            var vResult = _manager.Validate(_ChiTietKHGD);
            if (vResult.Valid)
            {
                var aResult = _manager.InsertOrUpdate(_ChiTietKHGD);
                if (aResult.Success)
                {
                    cha.mnuRefresh.PerformClick();
                    MessageBox.Show("Thêm thành công");
                    btnReset.PerformClick();
                    
                }
                else
                    MessageBox.Show(aResult.GetErrorMessages());
            }
            else MessageBox.Show(vResult.ToString());
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtMaKH.Text = string.Empty;
            mtbSoTietLTConLai.Text = "0";
            mtbSoTietLTDaDay.Text = "0";
            mtbSoTietTHConLai.Text = "0";
            mtbSoTietTHDaDay.Text = "0";
            mtbSoTuanConLai.Text = "0";
            cbbGhepLop.SelectedIndex = -1;

            _isUpdate = false;
            this.Text = "Thêm một chi tiết kế hoạch";
            txtMaKH.ReadOnly = _isUpdate;
        }

        private void btnThemLH_Click(object sender, EventArgs e)
        {
            frmQLLopHoc ql = new frmQLLopHoc();
            var frm = new frmAddLopHoc(new LopHocManager(LopHoc.Objects),ql);
            frm.Text = "Thêm lớp học";
            if (frm.ShowDialog() == DialogResult.OK)
            { LoadCbbGhepLop(); LoadCbbLopHoc(); }
                 
        }

        private void btnThemMonHoc_Click(object sender, EventArgs e)
        {
            frmQLMonHoc ql = new frmQLMonHoc();
            var frm = new frmAddMonHoc(new MonHocManager(MonHoc.Objects),ql);
            frm.Text = "Thêm môn học";
            if (frm.ShowDialog() == DialogResult.OK)
                LoadCbbMonHoc();
        }

        private void btnThemGV_Click(object sender, EventArgs e)
        {
            frmQLGiaoVien ql = new frmQLGiaoVien();
            var frm = new frmAddGiaoVien(new GiaoVienManager(GiaoVien.Objects),ql);
            frm.Text = "Thêm giáo viên";
            if (frm.ShowDialog() == DialogResult.OK)
                LoadCbbGiaoVien();
        }

       

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
